package bold.shiro;

import com.google.common.collect.Sets;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;

/**
 * 1、登录后处理
 * 2、第一次权限处理
 **/
@Slf4j
public class MyRealm extends AuthorizingRealm {

    /**
     * 设置权限
     */
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        log.info("shiro-trace==doGetAuthorizationInfo,{}",principalCollection);
        String username = principalCollection.getPrimaryPrincipal().toString();
        //todo 实现去查询逻辑获取权限
        SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
        info.setRoles(Sets.newHashSet("admin"));//角色
        info.setStringPermissions(Sets.newHashSet("perm1","perm2"));//权限属性
        return info;
    }

    /**
     * 登陆后
     * {}
     */
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authcToken) throws AuthenticationException {
        log.info("shiro-trace==doGetAuthenticationInfo");
        SimpleAuthenticationInfo authenticationInfo = new SimpleAuthenticationInfo(
                authcToken.getPrincipal(),
                "",
                this.getName());
        return authenticationInfo;
    }


}
